home *** CD-ROM | disk | FTP | other *** search
/ TeX 1995 July / TeX CD-ROM July 1995 (Disc 1)(Walnut Creek)(1995).ISO / macros / texinfo / info / search.c < prev    next >
C/C++ Source or Header  |  1994-01-28  |  15KB  |  567 lines

  1. /* search.c -- How to search large bodies of text. */
  2.  
  3. /* This file is part of GNU Info, a program for reading online documentation
  4.    stored in Info format.
  5.  
  6.    Copyright (C) 1993 Free Software Foundation, Inc.
  7.  
  8.    This program is free software; you can redistribute it and/or modify
  9.    it under the terms of the GNU General Public License as published by
  10.    the Free Software Foundation; either version 2, or (at your option)
  11.    any later version.
  12.  
  13.    This program is distributed in the hope that it will be useful,
  14.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16.    GNU General Public License for more details.
  17.  
  18.    You should have received a copy of the GNU General Public License
  19.    along with this program; if not, write to the Free Software
  20.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21.  
  22.    Written by Brian Fox (bfox@ai.mit.edu). */
  23.  
  24. #include <ctype.h>
  25. #include <sys/types.h>
  26. #include <sys/stat.h>
  27. #include "general.h"
  28. #include "search.h"
  29. #include "nodes.h"
  30.  
  31. #if !defined (NULL)
  32. #  define NULL 0x0
  33. #endif /* !NULL */
  34.  
  35. /* The search functions take two arguments:
  36.  
  37.      1) a string to search for, and
  38.  
  39.      2) a pointer to a SEARCH_BINDING which contains the buffer, start,
  40.         and end of the search.
  41.  
  42.    They return a long, which is the offset from the start of the buffer
  43.    at which the match was found.  An offset of -1 indicates failure. */
  44.  
  45. /* A function which makes a binding with buffer and bounds. */
  46. SEARCH_BINDING *
  47. make_binding (buffer, start, end)
  48.      char *buffer;
  49.      long start, end;
  50. {
  51.   SEARCH_BINDING *binding;
  52.  
  53.   binding = (SEARCH_BINDING *)xmalloc (sizeof (SEARCH_BINDING));
  54.   binding->buffer = buffer;
  55.   binding->start = start;
  56.   binding->end = end;
  57.   binding->flags = 0;
  58.  
  59.   return (binding);
  60. }
  61.  
  62. /* Make a copy of BINDING without duplicating the data. */
  63. SEARCH_BINDING *
  64. copy_binding (binding)
  65.      SEARCH_BINDING *binding;
  66. {
  67.   SEARCH_BINDING *copy;
  68.  
  69.   copy = make_binding (binding->buffer, binding->start, binding->end);
  70.   copy->flags = binding->flags;
  71.   return (copy);
  72. }
  73.  
  74.  
  75. /* **************************************************************** */
  76. /*                                    */
  77. /*           The Actual Searching Functions            */
  78. /*                                    */
  79. /* **************************************************************** */
  80.  
  81. /* Search forwards or backwards for the text delimited by BINDING.
  82.    The search is forwards if BINDING->start is greater than BINDING->end. */
  83. long
  84. search (string, binding)
  85.      char *string;
  86.      SEARCH_BINDING *binding;
  87. {
  88.   long result;
  89.  
  90.   /* If the search is backwards, then search backwards, otherwise forwards. */
  91.   if (binding->start > binding->end)
  92.     result = search_backward (string, binding);
  93.   else
  94.     result = search_forward (string, binding);
  95.  
  96.   return (result);
  97. }
  98.  
  99. /* Search forwards for STRING through the text delimited in BINDING. */
  100. long
  101. search_forward (string, binding)
  102.      char *string;
  103.      SEARCH_BINDING *binding;
  104. {
  105.   register int c, i, len;
  106.   register char *buff, *end;
  107.   char *alternate = (char *)NULL;
  108.  
  109.   len = strlen (string);
  110.  
  111.   /* We match characters in the search buffer against STRING and ALTERNATE.
  112.      ALTERNATE is a case reversed version of STRING; this is cheaper than
  113.      case folding each character before comparison.   Alternate is only
  114.      used if the case folding bit is turned on in the passed BINDING. */
  115.  
  116.   if (binding->flags & S_FoldCase)
  117.     {
  118.       alternate = savestring (string);
  119.  
  120.       for (i = 0; i < len; i++)
  121.     {
  122.       if (islower (alternate[i]))
  123.         alternate[i] = toupper (alternate[i]);
  124.       else if (isupper (alternate[i]))
  125.         alternate[i] = tolower (alternate[i]);
  126.     }
  127.     }
  128.  
  129.   buff = binding->buffer + binding->start;
  130.   end = binding->buffer + binding->end + 1;
  131.  
  132.   while (buff < (end - len))
  133.     {
  134.       for (i = 0; i < len; i++)
  135.     {
  136.       c = buff[i];
  137.  
  138.       if ((c != string[i]) && (!alternate || c != alternate[i]))
  139.         break;
  140.     }
  141.  
  142.       if (!string[i])
  143.     {
  144.       if (alternate)
  145.         free (alternate);
  146.       if (binding->flags & S_SkipDest)
  147.         buff += len;
  148.       return ((long) (buff - binding->buffer));
  149.     }
  150.  
  151.       buff++;
  152.     }
  153.  
  154.   if (alternate)
  155.     free (alternate);
  156.  
  157.   return ((long) -1);
  158. }
  159.  
  160. /* Search for STRING backwards through the text delimited in BINDING. */
  161. long
  162. search_backward (input_string, binding)
  163.      char *input_string;
  164.      SEARCH_BINDING *binding;
  165. {
  166.   register int c, i, len;
  167.   register char *buff, *end;
  168.   char *string;
  169.   char *alternate = (char *)NULL;
  170.  
  171.   len = strlen (input_string);
  172.  
  173.   /* Reverse the characters in the search string. */
  174.   string = (char *)xmalloc (1 + len);
  175.   for (c = 0, i = len - 1; input_string[c]; c++, i--)
  176.     string[i] = input_string[c];
  177.  
  178.   string[c] = '\0';
  179.  
  180.   /* We match characters in the search buffer against STRING and ALTERNATE.
  181.      ALTERNATE is a case reversed version of STRING; this is cheaper than
  182.      case folding each character before comparison.   ALTERNATE is only
  183.      used if the case folding bit is turned on in the passed BINDING. */
  184.  
  185.   if (binding->flags & S_FoldCase)
  186.     {
  187.       alternate = savestring (string);
  188.  
  189.       for (i = 0; i < len; i++)
  190.     {
  191.       if (islower (alternate[i]))
  192.         alternate[i] = toupper (alternate[i]);
  193.       else if (isupper (alternate[i]))
  194.         alternate[i] = tolower (alternate[i]);
  195.     }
  196.     }
  197.  
  198.   buff = binding->buffer + binding->start;
  199.   end = binding->buffer + binding->end;
  200.  
  201.   while (buff > end + len)
  202.     {
  203.       for (i = 0; i < len; i++)
  204.     {
  205.       c = *(buff - i);
  206.  
  207.       if (c != string[i] && (alternate && c != alternate[i]))
  208.         break;
  209.     }
  210.  
  211.       if (!string[i])
  212.     {
  213.       free (string);
  214.       if (alternate)
  215.         free (alternate);
  216.  
  217.       if (binding->flags & S_SkipDest)
  218.         buff -= len;
  219.       return ((long) (1 + (buff - binding->buffer)));
  220.     }
  221.  
  222.       buff--;
  223.     }
  224.  
  225.   free (string);
  226.   if (alternate)
  227.     free (alternate);
  228.  
  229.   return ((long) -1);
  230. }
  231.  
  232. /* Find STRING in LINE, returning the offset of the end of the string.
  233.    Return an offset of -1 if STRING does not appear in LINE.  The search
  234.    is bound by the end of the line (i.e., either NEWLINE or 0). */
  235. int
  236. string_in_line (string, line)
  237.      char *string, *line;
  238. {
  239.   register int end;
  240.   SEARCH_BINDING binding;
  241.  
  242.   /* Find the end of the line. */
  243.   for (end = 0; line[end] && line[end] != '\n'; end++);
  244.  
  245.   /* Search for STRING within these confines. */
  246.   binding.buffer = line;
  247.   binding.start = 0;
  248.   binding.end = end;
  249.   binding.flags = S_FoldCase | S_SkipDest;
  250.  
  251.   return (search_forward (string, &binding));
  252. }
  253.  
  254. /* Return non-zero if STRING is the first text to appear at BINDING. */
  255. int
  256. looking_at (string, binding)
  257.      char *string;
  258.      SEARCH_BINDING *binding;
  259. {
  260.   long search_end;
  261.  
  262.   search_end = search (string, binding);
  263.  
  264.   /* If the string was not found, SEARCH_END is -1.  If the string was found,
  265.      but not right away, SEARCH_END is != binding->start.  Otherwise, the
  266.      string was found at binding->start. */
  267.   return (search_end == binding->start);
  268. }
  269.  
  270. /* **************************************************************** */
  271. /*                                    */
  272. /*              Small String Searches                */
  273. /*                                    */
  274. /* **************************************************************** */
  275.  
  276. /* Function names that start with "skip" are passed a string, and return
  277.    an offset from the start of that string.  Function names that start
  278.    with "find" are passed a SEARCH_BINDING, and return an absolute position
  279.    marker of the item being searched for.  "Find" functions return a value
  280.    of -1 if the item being looked for couldn't be found. */
  281.  
  282. /* Return the index of the first non-whitespace character in STRING. */
  283. int
  284. skip_whitespace (string)
  285.      char *string;
  286. {
  287.   register int i;
  288.  
  289.   for (i = 0; string && whitespace (string[i]); i++);
  290.   return (i);
  291. }
  292.  
  293. /* Return the index of the first non-whitespace or newline character in
  294.    STRING. */
  295. int
  296. skip_whitespace_and_newlines (string)
  297.      char *string;
  298. {
  299.   register int i;
  300.  
  301.   for (i = 0; string && (whitespace (string[i]) || string[i] == '\n'); i++);
  302.   return (i);
  303. }
  304.  
  305. /* Return the index of the first whitespace character in STRING. */
  306. int
  307. skip_non_whitespace (string)
  308.      char *string;
  309. {
  310.   register int i;
  311.  
  312.   for (i = 0; string && !whitespace (string[i]); i++);
  313.   return (i);
  314. }
  315.  
  316. /* Return the index of the first non-node character in STRING.  Note that
  317.    this function contains quite a bit of hair to ignore periods in some
  318.    special cases.  This is because we here at GNU ship some info files which
  319.    contain nodenames that contain periods.  No such nodename can start with
  320.    a period, or continue with whitespace, newline, or ')' immediately following
  321.    the period.  If second argument NEWLINES_OKAY is non-zero, newlines should
  322.    be skipped while parsing out the nodename specification. */
  323. int
  324. skip_node_characters (string, newlines_okay)
  325.      char *string;
  326.      int newlines_okay;
  327. {
  328.   register int c, i = 0;
  329.   int paren_seen = 0;
  330.   int paren = 0;
  331.  
  332.   /* Handle special case.  This is when another function has parsed out the
  333.      filename component of the node name, and we just want to parse out the
  334.      nodename proper.  In that case, a period at the start of the nodename
  335.      indicates an empty nodename. */
  336.   if (string && *string == '.')
  337.     return (0);
  338.  
  339.   if (string && *string == '(')
  340.     {
  341.       paren++;
  342.       paren_seen++;
  343.       i++;
  344.     }
  345.  
  346.   for (; string && (c = string[i]); i++)
  347.     {
  348.       if (paren)
  349.     {
  350.       if (c == '(')
  351.         paren++;
  352.       else if (c == ')')
  353.         paren--;
  354.  
  355.       continue;
  356.     }
  357.       
  358.       /* If the character following the close paren is a space or period,
  359.      then this node name has no more characters associated with it. */
  360.       if (c == '\t' ||
  361.       c == ','  ||
  362.       c == INFO_TAGSEP ||
  363.       ((!newlines_okay) && (c == '\n')) ||
  364.       ((paren_seen && string[i - 1] == ')') &&
  365.        (c == ' ' || c == '.')) ||
  366.       (c == '.' &&
  367.        ((!string[i + 1]) ||
  368.         (whitespace_or_newline (string[i + 1])) ||
  369.         (string[i + 1] == ')'))))
  370.     break;
  371.     }
  372.   return (i);
  373. }
  374.  
  375. /* Unix doesn't have stricmp () functions. */
  376. int
  377. stricmp (string1, string2)
  378.      char *string1, *string2;
  379. {
  380.   char ch1, ch2;
  381.  
  382.   for (;;)
  383.     {
  384.       ch1 = *string1++;
  385.       ch2 = *string2++;
  386.  
  387.       if (!(ch1 | ch2))
  388.     return (0);
  389.  
  390.       ch1 = info_toupper (ch1);
  391.       ch2 = info_toupper (ch2);
  392.  
  393.       if (ch1 != ch2)
  394.     return (ch1 - ch2);
  395.     }
  396. }
  397.  
  398. /* Compare at most COUNT characters from string1 to string2.  Case
  399.    doesn't matter. */
  400. int
  401. strnicmp (string1, string2, count)
  402.      char *string1, *string2;
  403.      int count;
  404. {
  405.   register char ch1, ch2;
  406.  
  407.   while (count)
  408.     {
  409.       ch1 = *string1++;
  410.       ch2 = *string2++;
  411.  
  412.       ch1 = info_toupper (ch1);
  413.       ch2 = info_toupper (ch2);
  414.  
  415.       if (ch1 == ch2)
  416.     count--;
  417.       else
  418.     break;
  419.     }
  420.   return (count);
  421. }
  422.  
  423. /* **************************************************************** */
  424. /*                                    */
  425. /*             Searching FILE_BUFFER's                */
  426. /*                                    */
  427. /* **************************************************************** */
  428.  
  429. /* Return the absolute position of the first occurence of a node separator in
  430.    BINDING-buffer.  The search starts at BINDING->start.  Return -1 if no node
  431.    separator was found. */
  432. long
  433. find_node_separator (binding)
  434.      SEARCH_BINDING *binding;
  435. {
  436.   register long i;
  437.   char *body;
  438.  
  439.   body = binding->buffer;
  440.  
  441.   /* A node is started by [^L]^_[^L]\n.  That is to say, the C-l's are
  442.      optional, but the DELETE and NEWLINE are not.  This separator holds
  443.      true for all separated elements in an Info file, including the tags
  444.      table (if present) and the indirect tags table (if present). */
  445.   for (i = binding->start; i < binding->end - 1; i++)
  446.     if (((body[i] == INFO_FF && body[i + 1] == INFO_COOKIE) &&
  447.      (body[i + 2] == '\n' ||
  448.       (body[i + 2] == INFO_FF && body[i + 3] == '\n'))) ||
  449.     ((body[i] == INFO_COOKIE) &&
  450.      (body[i + 1] == '\n' ||
  451.       (body[i + 1] == INFO_FF && body[i + 2] == '\n'))))
  452.       return (i);
  453.   return (-1);
  454. }
  455.  
  456. /* Return the length of the node separator characters that BODY is
  457.    currently pointing at. */
  458. int
  459. skip_node_separator (body)
  460.      char *body;
  461. {
  462.   register int i;
  463.  
  464.   i = 0;
  465.  
  466.   if (body[i] == INFO_FF)
  467.     i++;
  468.  
  469.   if (body[i++] != INFO_COOKIE)
  470.     return (0);
  471.  
  472.   if (body[i] == INFO_FF)
  473.     i++;
  474.  
  475.   if (body[i++] != '\n')
  476.     return (0);
  477.  
  478.   return (i);
  479. }
  480.  
  481. /* Return the number of characters from STRING to the start of
  482.    the next line. */
  483. int
  484. skip_line (string)
  485.      char *string;
  486. {
  487.   register int i;
  488.  
  489.   for (i = 0; string && string[i] && string[i] != '\n'; i++);
  490.  
  491.   if (string[i] == '\n')
  492.     i++;
  493.  
  494.   return (i);
  495. }
  496.  
  497. /* Return the absolute position of the beginning of a tags table in this
  498.    binding starting the search at binding->start. */
  499. long
  500. find_tags_table (binding)
  501.      SEARCH_BINDING *binding;
  502. {
  503.   SEARCH_BINDING search;
  504.   long position;
  505.  
  506.   search.buffer = binding->buffer;
  507.   search.start = binding->start;
  508.   search.end = binding->end;
  509.   search.flags = S_FoldCase;
  510.  
  511.   while ((position = find_node_separator (&search)) != -1 )
  512.     {
  513.       search.start = position;
  514.       search.start += skip_node_separator (search.buffer + search.start);
  515.  
  516.       if (looking_at (TAGS_TABLE_BEG_LABEL, &search))
  517.     return (position);
  518.     }
  519.   return (-1);
  520. }
  521.  
  522. /* Return the absolute position of the node named NODENAME in BINDING.
  523.    This is a brute force search, and we wish to avoid it when possible.
  524.    This function is called when a tag (indirect or otherwise) doesn't
  525.    really point to the right node.  It returns the absolute position of
  526.    the separator preceding the node. */
  527. long
  528. find_node_in_binding (nodename, binding)
  529.      char *nodename;
  530.      SEARCH_BINDING *binding;
  531. {
  532.   register long position;
  533.   register int offset, namelen;
  534.   SEARCH_BINDING search;
  535.  
  536.   namelen = strlen (nodename);
  537.  
  538.   search.buffer = binding->buffer;
  539.   search.start = binding->start;
  540.   search.end = binding->end;
  541.   search.flags = 0;
  542.  
  543.   while ((position = find_node_separator (&search)) != -1)
  544.     {
  545.       search.start = position;
  546.       search.start += skip_node_separator (search.buffer + search.start);
  547.  
  548.       offset = string_in_line (INFO_NODE_LABEL, search.buffer + search.start);
  549.  
  550.       if (offset == -1)
  551.     continue;
  552.  
  553.       search.start += offset;
  554.       search.start += skip_whitespace (search.buffer + search.start);
  555.       offset = skip_node_characters
  556.     (search.buffer + search.start, DONT_SKIP_NEWLINES);
  557.  
  558.       /* Notice that this is an exact match.  You cannot grovel through
  559.      the buffer with this function looking for random nodes. */
  560.        if ((offset == namelen) &&
  561.        (search.buffer[search.start] == nodename[0]) &&
  562.        (strncmp (search.buffer + search.start, nodename, offset) == 0))
  563.      return (position);
  564.     }
  565.   return (-1);
  566. }
  567.